package com.gastronomy.controller;

import java.util.Currency;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gastronomy.entity.InformationEntity;
import com.gastronomy.entity.User;
import com.gastronomy.service.InformationService;
import com.gastronomy.service.UserService;
import com.gastronomy.util.SpringUtil;

@Controller
@RequestMapping("/adminstrotor")
public class AdminstrotorController {

	@Resource
	private UserService userService;
	
	@Resource
	private InformationService informationService;
	
	@RequestMapping("/adminstrotorHome")
	public String toAdminstrotorHome(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		User user = SpringUtil.getSessionUser(request);
		String currentTab = request.getParameter("currentTab");
		if(currentTab==null || (!currentTab.trim().equals("1") && !currentTab.trim().equals("2"))){
			currentTab= "1";
		}
		if(user.getRole().getId() !=2){
			
			return "/loing/error";
		}
		Integer [] roleTypes = {1,3};
		List<User> users  = userService.listUsers(roleTypes);
		
		List<InformationEntity> informationEntities = informationService.listInformations(1, Integer.MAX_VALUE);
		
		model.addAttribute("currentTab",currentTab);
		model.addAttribute("users",users);
		model.addAttribute("informationEntities",informationEntities);
		return "/adminstrotorPa/homePage";
	}
	
	
	@RequestMapping("/lockUser")
	@ResponseBody
	public Map<String, String> lockUser(HttpServletRequest request,
			HttpServletResponse response, Model model){
		User user = SpringUtil.getSessionUser(request);
		String ids = request.getParameter("ids");
		return userService.lockUser(ids,user);
	}

	@RequestMapping("/unLockUser")
	@ResponseBody
	public Map<String, String> unLockUser(HttpServletRequest request,
			HttpServletResponse response, Model model){
		User user = SpringUtil.getSessionUser(request);
		String ids = request.getParameter("ids");
		return userService.unLockUser(ids,user);
	}
	
	
	@RequestMapping("/addInformation")
	public String addInformation(HttpServletRequest request,
			HttpServletResponse response, Model model){
		
		User user = SpringUtil.getSessionUser(request);
		if( user.getRole().getId() !=2){
			model.addAttribute("errorMsg","您没有权限进行此操作");
			return "loginUser/error";
		}
		
		return "/adminstrotorPa/addInforPage";
	}
	
	
	@RequestMapping("/saveInformation")
	@ResponseBody
	public Map<String, String> saveInformation(HttpServletRequest request,
			HttpServletResponse response, InformationEntity informationEntity){
		User user = SpringUtil.getSessionUser(request);
		
		return informationService.saveInformation(user,informationEntity);
		
	}
	
}
